﻿using System.Collections.Generic;
using System.Linq;
using iHouse.Infrastructure;
using iHouse.Model.Data;
using iHouse.Model.Repository.Interface;

namespace iHouse.Model.Repository
{
    public class SqlPublisherRepository : BaseRepository<iHouseDbContext,Publisher>, IPublisherRepository
    {
        protected override string ContextKey
        {
            get { return "DataContextForPublisher"; }
        }

        #region IPublisherRepository Members


        public void DeletePublisher(int id)
        {
            Publisher p = CurrentDataContext.Publishers.Where(x => x.ID == id).First();

            CurrentDataContext.Publishers.Remove(p);
            CurrentDataContext.SaveChanges();
        }

        public void CreatePublisher(Publisher p)
        {
            CurrentDataContext.Publishers.Add(p);
            CurrentDataContext.SaveChanges();
        }

        public void UpdatePublisher(int id, string newName)
        {
            Publisher p = CurrentDataContext.Publishers.Where(x => x.ID == id).First();
            p.Name = newName;

            CurrentDataContext.SaveChanges();
        }

        public JqGridList<Publisher> GetPublishersForList(PagingCriteria criteria)
        {
            return CurrentDataContext.Publishers
                .OrderBy(x => x.ID)
                .ToJqGridList(criteria);
        }

        public IEnumerable<Publisher> GetPublishers()
        {
            return this.CurrentDataContext.Publishers
                .OrderBy(x => x.ID)
                .AsEnumerable();
        }

        #endregion
    }
}
